import request from './request';
import * as CryptoJS from 'crypto-js';
export * from './Is';
export * from './Storage';

export { request };

/**
 * 生成随机len位数字字符串
 */
export const randomString = (len, date) => {
  let random = '';
  random = Math.ceil(Math.random() * 1e14)
    .toString()
    .substr(0, len || 4);
  if (date) random = random + Date.now();
  return random;
};

/**
 *加密处理
 */
export const encryption = (params) => {
  let { data, type, param, key } = params;
  const result = JSON.parse(JSON.stringify(data));
  if (type === 'Base64') {
    param.forEach((ele) => {
      result[ele] = btoa(result[ele]);
    });
  } else {
    param.forEach((ele) => {
      var data = result[ele];
      key = CryptoJS.enc.Latin1.parse(key);
      var iv = key;
      // 加密
      var encrypted = CryptoJS.AES.encrypt(data, key, {
        iv: iv,
        mode: CryptoJS.mode.CFB,
        padding: CryptoJS.pad.NoPadding,
      });
      result[ele] = encrypted.toString();
    });
  }
  return result;
};

// 获取链接字符串上面的指定字段的值
export function getParamFromUrlStr(str, name) {
  let query = str.split('?')[1];
  let vars = query.split('&');
  for (let i = 0; i < vars.length; i++) {
    let pair = vars[i].split('=');
    if (pair[0] === name) {
      return pair[1];
    }
  }
  return '';
}
